import profile
import tkinter.simpledialog
import helpers.FileHelper as FileHelper
import helpers.MiscHelper as MiscHelper
import helpers.GUIHelper as GUIHelper
import package.PackageManager as PackageManager


def cmd_import(opts, args):
    src = GUIHelper.ask_load()
    dst = profile.effect_workspace
    tmp = FileHelper.solve_path(profile.effect_temporary, "import.zip")
    FileHelper.zip(dst, tmp)
    FileHelper.uzp(src, dst)


def cmd_smart_import(opts, args):
    opts, args = MiscHelper.merge_args(profile.effect_import_args, [], opts, args)
    src = GUIHelper.ask_load()
    dst = profile.effect_workspace
    tmp = FileHelper.solve_path(profile.effect_temporary, "import.zip")
    FileHelper.zip(dst, tmp)
    FileHelper.uzp(src, dst)
    pkg = PackageManager.parse(dst)
    pkg.setup(opts)


def cmd_export(opts, args):
    src = profile.effect_workspace
    dst = GUIHelper.ask_save()
    FileHelper.cp(dst if dst.endswith(".zip") else dst+".zip", (profile.effect_temporary, "export.zip"))
    FileHelper.zip(src, dst)


def cmd_smart_export(opts, args):
    opts, args = MiscHelper.merge_args(profile.effect_export_args, [], opts, args)
    src = profile.effect_workspace
    dst = GUIHelper.ask_save()
    pkg = PackageManager.parse(src)
    pkg.setup(opts)
    FileHelper.cp(dst if dst.endswith(".zip") else dst+".zip", (profile.effect_temporary, "export.zip"))
    FileHelper.zip(src, dst)


def cmd_upload(opts, args):
    opts, args = MiscHelper.merge_args({"m": None}, [], opts, args)
    pkg = PackageManager.parse(profile.effect_workspace)
    pkg.setup(opts)
    print(opts)
    print(args)
    PackageManager.upload_package(int(args[0]) if args else 0)


def cmd_workspace(data, opts, args):
    opts, args = MiscHelper.merge_args(data, [], opts, args)
    pkg = PackageManager.parse(profile.effect_workspace)
    pkg.setup(opts)


def cmd_cache(opts, args):
    for k, v in opts:
        if k in ("c", "clear"):
            PackageManager.cache_clear()
        elif k in ("a", "push"):
            PackageManager.cache_push(v)
        elif k in ("d", "pop"):
            PackageManager.cache_pop()
        elif k in ("s", "swap"):
            PackageManager.cache_swap(v)
    PackageManager.cache_print()


def cmd_mv_render(opts, args):
    opts, args = MiscHelper.merge_args({"m": None, "D": "DEV=1"}, [], opts, args)
    pkg = PackageManager.parse(profile.effect_workspace)
    pkg.setup(opts)
    PackageManager.mv_render(len(args) > 0 and args[0] == "s")


def cmd_cc_render(opts, args):
    opts, args = MiscHelper.merge_args({"m": None, "D": ""}, [], opts, args)
    pkg = PackageManager.parse(profile.effect_workspace)
    pkg.setup(opts)
    PackageManager.cc_render()


CMD_SET = {
    "I": cmd_import,
    "i": cmd_smart_import,
    "E": cmd_export,
    "e": cmd_smart_export,
    "u": cmd_upload,
    "c": cmd_cache,
    "m": cmd_mv_render,
    "j": cmd_cc_render,
}


def main(name, opts, args):
    custom_cmd_set = profile.custom_command_set
    if name in custom_cmd_set:
        data = custom_cmd_set[name]
        if callable(data):
            data(opts, args)
        else:
            cmd_workspace(data, opts, args)
    elif name in CMD_SET:
        CMD_SET[name](opts, args)


if __name__ == "__main__":
    tk = tkinter.Tk()
    tk.withdraw()
    data_ = tkinter.simpledialog.askstring("bytedance", prompt="Input command:")
    if data_ is None:
        exit(0)

    data_ = data_.split(maxsplit=1)
    name_ = data_[0] if len(data_) > 0 else ""
    opts_, args_ = MiscHelper.parse_args(data_[1] if len(data_) == 2 else "")
    main(name_, opts_, args_)
